package com.situ.web.servlet;

import com.situ.web.pojo.Course;
import com.situ.web.pojo.query.CourseQuery;
import com.situ.web.service.ICourseService;
import com.situ.web.service.impl.CourseServiceImpl;
import com.situ.web.util.JSONUtil;
import com.situ.web.util.PageResult;
import com.situ.web.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {
    private ICourseService courseService = new CourseServiceImpl();

    // /course?method=selectByPage&pageNo=1
    // /course?method=deleteById&id=12
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CourseServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");
        Course course = new Course();
        course.setId(Integer.parseInt(id));
        course.setName(name);
        course.setCredit(Integer.parseInt(credit));
        courseService.update(course);

        toJSON(resp, Result.ok("修改成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CourseServlet.selectById");
        String id = req.getParameter("id");
        Course course = courseService.selectById(Integer.parseInt(id));
        //Result result = new Result(0, course);
        toJSON(resp, Result.ok(course));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CourseServlet.add");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");
        Course course = new Course();
        course.setName(name);
        course.setCredit(Integer.parseInt(credit));
        courseService.add(course);

        Result result = new Result(0, "添加成功");
        toJSON(resp, result);
    }


    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String[] ids = req.getParameterValues("ids[]");
        courseService.deleteAll(ids);

        Result result = new Result(0, "删除成功");
        toJSON(resp, result);
        //JSONUtil.toJSON(resp, result);

       /* resp.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(), result);*/
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        courseService.deleteById(Integer.parseInt(id));

        //1.成功、失败 code
        //2.消息      msg
        //3.数据      data    Map、Object
        Result result = new Result(0, "删除成功");
        JSONUtil.toJSON(resp, result);
        /*resp.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(), result);*/
    }

    // http://localhost:8080/course?method=selectByPage&page=1&limit=10&name=zhangsan&credit=12
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");
        CourseQuery courseQuery = new CourseQuery(Integer.parseInt(page), Integer.parseInt(limit), name, credit);
        PageResult<Course> pageResult = courseService.selectByPage(courseQuery);

        JSONUtil.toJSON(resp, pageResult);
        /*resp.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(), pageResult);*/
    }

    // /course?method=selectByPage&pageNo=1&pageSize=5
    /*private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.selectByPage");
        String pageNo = req.getParameter("pageNo");
        if (pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        String pageSize = req.getParameter("pageSize");
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "3";
        }
        PageInfo<Course> pageInfo = courseService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        System.out.println(pageInfo);

        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("/course_list.jsp").forward(req, resp);
    }*/
}
